package org.jeecg.modules.demo.student.service.impl;

import org.jeecg.modules.demo.student.entity.PzzSchoolStudent;
import org.jeecg.modules.demo.student.entity.PzzStudentGrade;
import org.jeecg.modules.demo.student.mapper.PzzStudentGradeMapper;
import org.jeecg.modules.demo.student.mapper.PzzSchoolStudentMapper;
import org.jeecg.modules.demo.student.service.IPzzSchoolStudentService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 学生表
 * @Author: jeecg-boot
 * @Date:   2021-08-13
 * @Version: V1.0
 */
@Service
public class PzzSchoolStudentServiceImpl extends ServiceImpl<PzzSchoolStudentMapper, PzzSchoolStudent> implements IPzzSchoolStudentService {

	@Autowired
	private PzzSchoolStudentMapper pzzSchoolStudentMapper;
	@Autowired
	private PzzStudentGradeMapper pzzStudentGradeMapper;
	
	@Override
	@Transactional
	public void saveMain(PzzSchoolStudent pzzSchoolStudent, List<PzzStudentGrade> pzzStudentGradeList) {
		pzzSchoolStudentMapper.insert(pzzSchoolStudent);
		if(pzzStudentGradeList!=null && pzzStudentGradeList.size()>0) {
			for(PzzStudentGrade entity:pzzStudentGradeList) {
				//外键设置
				entity.setMainId(pzzSchoolStudent.getId());
				pzzStudentGradeMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(PzzSchoolStudent pzzSchoolStudent,List<PzzStudentGrade> pzzStudentGradeList) {
		pzzSchoolStudentMapper.updateById(pzzSchoolStudent);
		
		//1.先删除子表数据
		pzzStudentGradeMapper.deleteByMainId(pzzSchoolStudent.getId());
		
		//2.子表数据重新插入
		if(pzzStudentGradeList!=null && pzzStudentGradeList.size()>0) {
			for(PzzStudentGrade entity:pzzStudentGradeList) {
				//外键设置
				entity.setMainId(pzzSchoolStudent.getId());
				pzzStudentGradeMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		pzzStudentGradeMapper.deleteByMainId(id);
		pzzSchoolStudentMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			pzzStudentGradeMapper.deleteByMainId(id.toString());
			pzzSchoolStudentMapper.deleteById(id);
		}
	}
	
}
